<?php

namespace App\Containers\AppSection\User\Models;

use App\Containers\AppSection\Authentication\Traits\AuthenticationTrait;
use App\Containers\AppSection\Authorization\Models\Role;
use App\Containers\AppSection\Authorization\Traits\AuthorizationTrait;
use App\Containers\AppSection\Ban\Traits\HasBansModel;
use App\Containers\AppSection\Profile\Models\Profile;
use App\Containers\AppSection\Status\Models\Status;
use App\Ship\Parents\Models\UserModel;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;

/**
 * Пользователь
 * @package App\Models
 *
 * @property int $id Id
 * @property string $fio Имя
 * @property string $email Почта
 * @property string $phone Телефон
 * @property string $phone_verified_at Время подтверждения телефона
 * @property string $password Пароль
 * @property string $device
 * @property string $platform
 * @property string $ban_id Бан
 * @property string $status_key Статус
 * @property string $social_provider
 * @property string $social_token
 * @property string $social_refresh_token
 * @property string $social_expires_in
 * @property string $social_token_secret
 * @property string $social_id
 * @property string $social_avatar
 * @property string $social_avatar_original
 * @property string $social_nickname
 * @property string $email_verified_at Время подтверждения почты
 * @property string $other_information Иная информация
 * @property Carbon $created_at Время создания
 * @property Carbon $updated_at Время обновления
 *
 * @property Status $status Статус
 * @property Profile $profiles Профили
 * @property Role[] $roles Роли
 */
class User extends UserModel
{
    use HasBansModel;
    use AuthorizationTrait;
    use AuthenticationTrait;
    use Notifiable;
    use SoftDeletes;

    protected $table = 'users';

    protected $fillable = [
        'fio',
        'email',
        'password',
        'device',
        'platform',
        'status_key',
        'social_provider',
        'social_token',
        'social_refresh_token',
        'social_expires_in',
        'social_token_secret',
        'social_id',
        'social_avatar',
        'social_avatar_original',
        'social_nickname',
        'email_verified_at',
        'ban_id',
        'phone',
        'phone_verified_at',
        'other_information',
        'deleted_at'
    ];

    protected $hidden = [
        'password',
        'remember_token',
    ];

    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    protected string $banKey = 'profile_id';

    /**
     * Профили
     * @return HasMany|Profile Profiles
     */
    public function profiles(): HasMany
    {
        return $this->hasMany(Profile::class, 'user_id', 'id');
    }

    /**
     * A model may have profile.
     */
    public function profile(): HasMany
    {
        return $this->profiles()->limit(1);
    }

    /**
     * Статус
     * @return BelongsTo Status
     */
    public function status(): BelongsTo
    {
        return $this->belongsTo(Status::class, 'status_key', 'key');
    }

    /**
     * A model may have role.
     */
    public function role(): BelongsToMany
    {
        return $this->roles()->limit(1);
    }

    public function hasRoleType($type): bool
    {
        $role = $this->roles()->first();

        if (empty($role)) return false;

        return $role->role_type === $type;
    }
}
